#include "conuseritem.h"
#include "ui_conuseritem.h"

ConUserItem::ConUserItem(QWidget *parent)
    : ListItemBase(parent)
    , ui(new Ui::ConUserItem)
    , m_user_info(nullptr)
{
    ui->setupUi(this);

    setItemType(ListItemType::CONTACT_USER_ITEM);
    ui->red_point_lb->raise();
    showRedPoint(false);
}

ConUserItem::~ConUserItem()
{
    delete ui;
}

QSize ConUserItem::sizeHint() const
{
    return QSize(250, 70); // 返回自定义的尺寸
}

void ConUserItem::setInfo(std::shared_ptr<AuthInfo> auth_info)
{
    m_user_info = std::make_shared<UserInfo>(auth_info);

    // 加载图片
    QPixmap pixmap(m_user_info->m_icon);

    // 设置图片自动缩放
    ui->icon_lb->setPixmap(pixmap.scaled(ui->icon_lb->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
    ui->icon_lb->setScaledContents(true);

    ui->user_name_lb->setText(m_user_info->m_name);
}

void ConUserItem::setInfo(std::shared_ptr<AuthRsp> auth_rsp)
{
    m_user_info = std::make_shared<UserInfo>(auth_rsp);

    // 加载图片
    QPixmap pixmap(m_user_info->m_icon);

    // 设置图片自动缩放
    ui->icon_lb->setPixmap(pixmap.scaled(ui->icon_lb->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
    ui->icon_lb->setScaledContents(true);

    ui->user_name_lb->setText(m_user_info->m_name);
}

void ConUserItem::setInfo(int uid, QString name, QString icon)
{
    m_user_info = std::make_shared<UserInfo>(uid, name, icon);

    // 加载图片
    QPixmap pixmap(m_user_info->m_icon);

    // 设置图片自动缩放
    ui->icon_lb->setPixmap(pixmap.scaled(ui->icon_lb->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
    ui->icon_lb->setScaledContents(true);

    ui->user_name_lb->setText(m_user_info->m_name);
}

void ConUserItem::showRedPoint(bool show)
{
    if (show) {
        ui->red_point_lb->show();
    } else{
        ui->red_point_lb->hide();
    }
}

std::shared_ptr<UserInfo> ConUserItem::getInfo()
{
    return m_user_info;
}
